package com.tg.app.activity.base;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tg.app.R;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.app.adapter.SettingAdapter;
import com.tg.data.bean.DeviceSettingsInfo;
import com.appbase.custom.base.SettingData;


import java.util.ArrayList;
import java.util.List;

/**
 * Describe:
 * Created by Rance Rang on 2019-06-04.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
public abstract class DeviceSettingsListBaseActivity extends  DeviceSettingsBaseActivity {


    private ArrayList<SettingData>  dataList = new ArrayList<>();
    private ListView    listView ;
    private SettingAdapter   adapter;
    private TextView  tvTextView;
    private RelativeLayout layout;
    public void setTitle(String  title) {
        this.tvTextView.setText(title);
    }
    public void setTitle(int  redId) {
        this.tvTextView.setText(redId);
    }
    public List<SettingData> getDataList() {
        return dataList;
    }
    public void setDataList(ArrayList<SettingData> dataList) {
        this.dataList.clear();
        this.dataList.addAll(dataList);
        this.adapter.notifyDataSetChanged();
    }

    public ListView getListView() {
        return listView;
    }

    public SettingAdapter getAdapter() {
        return adapter;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        hideActionBar();
        setContentView(R.layout.device_settings_list_base);
        layout = findViewById(R.id.list_layout);
        listView = findViewById(R.id.settings_list);
        tvTextView  = findViewById(R.id.device_name);
        adapter = new SettingAdapter(dataList,this);
        listView.setAdapter(adapter);
        modifyToolBar();
        backClickEvent();
    }

    public  void sendUpdateSettingBroadcast(DeviceSettingsInfo info){
        Intent intent = new Intent();
        intent.setAction(DeviceSettingsActivity.ACTION_DEVICE_SETTINGS);
        intent.putExtra(DeviceSettingsActivity.EXT_DEVICE_INFO,info);
        sendBroadcast(intent);
    }

    public void backClickEvent(){
        findViewById(R.id.back_toolbar).setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                mHandler.removeCallbacks(timeRunable);
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        mHandler.removeCallbacks(timeRunable);
        finish();
    }
    public void setFlag(int position){
        hideLoading();
        for(int i = 0;i<dataList.size();i++){
            if(i == position){
                dataList.get(i).setFlag(1);
            }else{
                dataList.get(i).setFlag(0);
            }
        }
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });

    }
    protected void updateUI(){
        layout.setBackgroundColor(getResources().getColor(R.color.settings_bg_color));
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) listView.getLayoutParams();
        layoutParams.topMargin =-1;
        listView.setLayoutParams(layoutParams);
    }
}
